package com.todo.gateway.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.util.pattern.PathPatternParser;

/**
 * @author azhebuxing
 * @date 2025/2/22 01:15
 * @description
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter() {
        CorsConfiguration corsConfig = new CorsConfiguration();
        corsConfig.setAllowCredentials(true); // 允许凭据
        corsConfig.addAllowedOriginPattern("*"); // 使用 allowedOriginPatterns
        corsConfig.addAllowedHeader("*"); // 允许所有请求头
        corsConfig.addAllowedMethod("*"); // 允许所有 HTTP 方法

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        source.registerCorsConfiguration("/**", corsConfig); // 对所有路径生效

        return new CorsWebFilter(source);
    }

}
